import { ACCESS_TOKEN } from '@/store/mutation-types'
import VueCookie from 'vue-cookie'

const user = {
  state: {
    access_token: '',
    userInfo: {
      signature: '',
      name: '',
      avatar: '',
      username: ''
    },
    isLogin: false
  },
  mutations: {
    setAccessToken: (state, access_token) => {
      state.access_token = access_token
      // 放入cookie
      VueCookie.set(ACCESS_TOKEN, access_token)
    },
    setUserInfo: (state, userInfo) => {
      state.userInfo.signature = userInfo.signature
      state.userInfo.name = userInfo.realname
      state.userInfo.avatar = userInfo.avatar
      state.userInfo.username = userInfo.username
      VueCookie.set('userInfo', JSON.stringify(userInfo))
    },
    clearUserInfo: (state) => {
      state.userInfo = {}
      VueCookie.delete('userInfo')
    },
    setIsLogin: (state, isLogin) => {
      state.isLogin = isLogin
    }
  }
}

export default user
